import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseGuards,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { QueryPageDto } from '../../common/dto/query.page.dto';
import { QueryUserDto } from './dto/query-user.dto';
import { RolesGuard } from '../../common/guards/roles.guard';
import { AuthGuard } from '@nestjs/passport';
import { ResetUserDto } from './dto/reset-user.dto';
import { ApiQueryPage } from '../../common/decorator/response.decorator';
import { Roles } from '../../common/decorator/roles.decorator';
import { Role } from '../../common/types/role';

@Controller('user')
@ApiTags('[ADMIN]用户管理')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: '创建用户' })
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Post('page')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @ApiQueryPage(QueryUserDto)
  @ApiOperation({ summary: '分页用户' })
  findPage(@Body() query: QueryPageDto<QueryUserDto>) {
    return this.userService.findByPage(query);
  }

  @Get(':id')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: '获取用户详情' })
  findOne(@Param('id') id: string) {
    return this.userService.findOne(id);
  }

  @Put(':id')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: '更新用户' })
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(id, updateUserDto);
  }

  @Delete(':id')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: '删除用户' })
  remove(@Param('id') id: string) {
    return this.userService.remove(id);
  }

  @Put(':id/password')
  @Roles(Role.ADMIN)
  @UseGuards(RolesGuard)
  @ApiOperation({ summary: '重置用户密码' })
  resetPassword(@Body() form: ResetUserDto, @Param('id') id: string) {
    return this.userService.resetPassword(id, form);
  }

  @Get()
  @ApiOperation({ summary: '获取所有用户' })
  findAll() {
    return this.userService.findAll();
  }
}
